package interpreter.builtin.functions;

import interpreter.Callable;

import java.util.ArrayList;
import java.util.List;

import math.ComplexNumber;

public class MinusFunction implements Callable {

	private PlusFunction plus = new PlusFunction();

	@Override
	public Object call(List<Object> arguments) {
		if (arguments.size() == 1)
			return ((ComplexNumber) arguments.get(0)).negate();
		ComplexNumber sum = sumAllButFirst(arguments);
		return plus.call(makeList(arguments.get(0), sum.negate()));
	}

	private List<Object> makeList(Object arg1, Object arg2) {
		List<Object> args = new ArrayList<>();
		args.add(arg1);
		args.add(arg2);
		return args;
	}

	private ComplexNumber sumAllButFirst(List<Object> arguments) {
		List<Object> lastArgs = arguments.subList(1, arguments.size());
		return (ComplexNumber) plus.call(lastArgs);
	}
}
